package com.aj.service;

import com.aj.Interface.RoleService;
import com.aj.dao.RoleDao;
import com.aj.dao.UserDao;
import com.aj.pojo.Role;
import com.aj.pojo.Setmeal;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.PageResult;
import entity.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private UserDao userDao;

  @Override
    public PageResult findpage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> page = roleDao.findpage(queryPageBean.getQueryString());
        PageResult result =  new PageResult(page.getTotal(),page.getResult());
        return result;
    }

    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //首先添加角色
        roleDao.add(role);
        System.out.println(role.getId());

        //添加角色与权限的中间表
        if (permissionIds!=null&&permissionIds.length>0){
            for (Integer permissionId : permissionIds) {
                HashMap<Object, Object> map = new HashMap<>();
                map.put("roleId",role.getId());
                map.put("permissionId",permissionId);
                roleDao.add_Role_Permission(map);
            }
        }

        //添加角色与菜单表的中间表
        if (menuIds!=null&&menuIds.length>0){
            for (Integer menuId : menuIds) {
                HashMap<Object, Object> map = new HashMap<>();
                map.put("roleId",role.getId());
                map.put("menuId",menuId);
                roleDao.add_Role_Menu(map);
            }
        }
    }

    @Override
    public void delete(Integer id) {
        //先查询该角色是否被用户绑定
        int count = userDao.findCountByRoleId(id);
        if (count>0){
            throw new RuntimeException("该角色被用户绑定,请解绑后再删除");
        }

        //删除与权限的中间表
        roleDao.delete_role_permission_ByRoleId(id);

        //删除与菜单的中间表
        roleDao.delete_role_menu_ByRoleId(id);

        //删除角色
        roleDao.delete(id);
    }

    @Override
    public Role findRoleById(Integer id) {
        return roleDao.findRoleById(id);
    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        return roleDao.findPermissionIdsByRoleId(id);
    }
    public List<Role> findall() {
        List<Role> rolelist =  roleDao.findall();
        return rolelist;
    }

    @Override
    public List<Integer> findMenuIdsBySetmealid(Integer id) {
        return roleDao.findMenuIdsBySetmealid(id);
    }

    @Override
    public void editTrue(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.updateRole(role);
        if (permissionIds!=null&&permissionIds.length>0){
            roleDao.delete_role_permission_ByRoleId(role.getId());
            for (Integer permissionId : permissionIds) {
                Map map = new HashMap();
                map.put("roleId", role.getId());
                map.put("permissionId",permissionId);
                roleDao.add_Role_Permission(map);
            }
        }
        if (menuIds!=null&&menuIds.length>0){
            roleDao.delete_role_menu_ByRoleId(role.getId());
            for (Integer menuId : menuIds) {
                Map map = new HashMap();
                map.put("roleId", role.getId());
                map.put("menuId",menuId);
                roleDao.add_Role_Menu(map);
            }
        }
    }
}
